import { MCPServer, MCPServerConfig } from "mcp-framework";

export class StdioMCPServer {
    private server: MCPServer;

    constructor(config: MCPServerConfig) {
        if (config.transport?.type !== 'stdio') {
            throw new Error('config.transport.type 必须是 stdio');
        }
        this.server = new MCPServer(config);

        process.on('SIGINT', () => this.shutdown());
        process.on('SIGTERM', () => this.shutdown());
    }

    async start() {
        try {
            await this.server.start();
            console.error('服务启动成功');
        } catch (error) {
            console.error('服务启动失败:', error);
            process.exit(1);
        }
    }

    private async shutdown() {
        console.error('服务关闭中...');
        try {
            await this.server.stop();
            process.exit(0);
        } catch (error) {
            console.error('服务关闭失败:', error);
            process.exit(1);
        }
    }
}
